package com.example.demo.controller;

//在controller层不要出现domain实体category

import com.example.demo.req.CategoryQueryReq;
import com.example.demo.req.CategorySaveReq;
import com.example.demo.resp.CategoryQueryResp;
import com.example.demo.resp.CommonResp;
import com.example.demo.resp.PageResp;
import com.example.demo.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {



    @Resource
    private CategoryService categoryService;

    @GetMapping("/all")
    public CommonResp all(){
        CommonResp<List<CategoryQueryResp>> resp=new CommonResp<>();
        List <CategoryQueryResp> list=categoryService.all();
        resp.setContent(list);
        return resp;
    }

        @GetMapping("/list")
        public CommonResp list(@Valid CategoryQueryReq req){
            CommonResp<PageResp<CategoryQueryResp>> resp=new CommonResp<>();
            PageResp<CategoryQueryResp> list=categoryService.list(req);
            resp.setContent(list);
            return resp;
        }

        /**
         * @RequestBody
         *   对应jsaon方式的post请求
         *
         *   ptrp  form表单就不用注解
         * */
    @PostMapping("/save")
    public CommonResp save(@RequestBody CategorySaveReq req){
        CommonResp resp=new CommonResp<>();
       categoryService.save(req);

        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id){
        CommonResp resp=new CommonResp<>();
        categoryService.delete(id);

        return resp;
    }

}
